<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="" prop="templateName">
                    <el-input class="w-[280px]" v-model="queryParams.keyword" placeholder="模版名称/模版内容"/>
                </el-form-item>
                <el-form-item label="" prop="templateState">
                    <el-select v-model="queryParams.templateState" class="w-[280px]" clearable placeholder="模版状态">
                        <el-option
                            v-for="(item, index) in dictData.status"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间" prop="creationTime">
                    <daterange-picker
                        v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button v-perms="['template:add']" type="primary" @click="handleAdd()">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="签署方" prop="contractType" min-width="100">
                    <template #default="{ row }">
                        <dict-value :options="dictData.contract_type" :value="row.contractType" />
                    </template>
                </el-table-column>
                <el-table-column label="模板名称" prop="templateName" min-width="200" />
                <el-table-column label="模板内容" prop="templateContent" min-width="200" />
                <el-table-column label="状态" prop="templateState" min-width="100">
                    <template #default="{ row }">
                        <el-switch
                            v-perms="['contract_template_state:disable']"
                            :model-value="row.templateState"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeTemplateStatus($event, row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="显示状态" prop="state" min-width="100">
                    <template #default="{ row }">
                        <el-switch
                            v-perms="['contract_state:disable']"
                            :model-value="row.state"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus($event, row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['template:edit']" type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button v-perms="['template:del']" type="danger" link @click="handleDelete(row.templateNumber)">
                            删除
                        </el-button>
                        <el-button v-perms="['template:del']" type="primary" link @click="handleDetail(row)">
                            查看
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="template">
import { templateDelete, templateLists, templateEditState } from '@/api/contract_template'
import { useDictData } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {adminStatus} from "@/api/perms/admin";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    keyword: '',
    templateState: '',
    createTimeStart: '',
    createTimeEnd: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: templateLists,
    params: queryParams
})

const { dictData } = useDictData<{
    contract_type: any[]
    status: any[]
}>(['contract_type','status'])


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDetail = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('detail')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await templateDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const changeTemplateStatus = async (active: any, row: object) => {
    try {
        await feedback.confirm(`确定${active ? '启用' : '禁用'}当前模版状态？`)
        await templateEditState({ templateNumber: row.templateNumber, templateState: active, state: row.state })
        feedback.msgSuccess('修改成功')
        getLists()
    } catch (error) {
        getLists()
    }
}

const changeStatus = async (active: any, row: object) => {
    try {
        await feedback.confirm(`确定${active ? '启用' : '禁用'}当前模版显示状态？`)
        await templateEditState({ templateNumber: row.templateNumber, state: active, templateState: row.templateState })
        feedback.msgSuccess('修改成功')
        getLists()
    } catch (error) {
        getLists()
    }
}


getLists()
</script>
